package redecolaborativa.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.CaptionPanel;

public class PesquisaItens extends Composite {
	private AbsolutePanel absolutePanel, absolutePanelCaption;
	private FachadaAsync fachada;
	private String idSessao, visualizador;
	private final ScrollPanel itensEncontrados, painelPrincipal, painelGeral;	
	
	
	public PesquisaItens(ScrollPanel painelGeral, final ScrollPanel painelPrincipal, String visualizador, final FachadaAsync fachada, final String idSessao) {
		this.fachada = fachada;
		this.idSessao = idSessao;
		this.visualizador = visualizador;
		this.absolutePanel = new AbsolutePanel();
		absolutePanelCaption = new AbsolutePanel();
		this.painelPrincipal = painelPrincipal;
		this.painelGeral = painelGeral;
		
		initWidget(absolutePanel);
		absolutePanel.setSize("701px", "448px");
		iniciaLabels();
		
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("Pesquisa de itens");
		cptnpnlNewPanel.setStyleName("caption-panel");
		absolutePanel.add(cptnpnlNewPanel, 24, 0);
		cptnpnlNewPanel.setSize("643px", "430px");
	
		cptnpnlNewPanel.setContentWidget(absolutePanelCaption);
		absolutePanelCaption.setSize("648px", "408px");
		
		final ListBox ordenacao = new ListBox();
		absolutePanelCaption.add(ordenacao, 478, 12);
		ordenacao.setSize("157px", "20px");
		ordenacao.addItem("crescente");
		ordenacao.addItem("decrescente");
		
		final ListBox criterio = new ListBox();
		absolutePanelCaption.add(criterio, 478, 53);
		criterio.setSize("157px", "20px");
		criterio.addItem("datacriacao");
		criterio.addItem("reputacao");
		
		final TextBox chave = new TextBox();
		absolutePanelCaption.add(chave, 120, 11);
		chave.setSize("191px", "16px");
		
		final ListBox atributo = new ListBox();
		absolutePanelCaption.add(atributo, 120, 53);
		atributo.setSize("157px", "20px");
		atributo.addItem("nome");
		atributo.addItem("descricao");
		atributo.addItem("categoria");
		
		this.itensEncontrados = new ScrollPanel();
		absolutePanelCaption.add(itensEncontrados, 10, 127);
		itensEncontrados.setSize("632px", "281px");
		
		Button btnPesquisar = new Button("Pesquisar");
		absolutePanelCaption.add(btnPesquisar, 456, 93);
		btnPesquisar.setSize("157px", "28px");
		btnPesquisar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				itensEncontrados.clear();
				try {
					pesquisarItens(chave, atributo, criterio, ordenacao);
				} catch (Exception e) {
					Window.alert(e.getMessage());
				}
			}
		});
	}
	
	private void pesquisarItens(TextBox chave, final ListBox atributo, ListBox criterio, ListBox ordenacao) throws Exception {
		fachada.pesquisaItens(idSessao, chave.getText(), atributo.getItemText(atributo.getSelectedIndex()), ordenacao.getItemText(ordenacao.getSelectedIndex()), 
		criterio.getItemText(criterio.getSelectedIndex()), new AsyncCallback<List<String>>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			} @Override
			public void onSuccess(List<String> result) {
				if (result.isEmpty()) {
					itensEncontrados.add(new Label("Nenhum item encontrado!"));
				} else {
					mostraResultados(result);
				}
			}
		});
	}

	private void mostraResultados(List<String> result) {
		final AbsolutePanel panel = new AbsolutePanel();
		for (final String nomeItem: result) {
			fachada.getItem(idSessao, nomeItem, new AsyncCallback<String>() {
				@Override
				public void onFailure(Throwable caught) { }
				@Override
				public void onSuccess(final String result) {
					String nome = result.split("-")[0];
					String descricao = result.split("-")[1];
					String categoria = result.split("-")[2];
					String dono = result.split("-")[3];
					String nota = result.split("-")[4];
					
					Label itemClick = new Label("Item: " + nome + "      Categoria: " + categoria);
					Label descricaoItem = new Label("Descricao: " + descricao);
					Label donoItem = new Label("Dono: " + dono);
					Label ultimaNota = new Label("Ultima nota atribuida ao item: " + nota);
					
					eventoDeMouseOver(itemClick);
					panel.add(itemClick);
					panel.add(descricaoItem);
					panel.add(donoItem);
					panel.add(ultimaNota);
					
					itemClick.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							painelPrincipal.clear();
							painelPrincipal.add(new VisualizarItem(painelGeral, visualizador, result, idSessao, nomeItem, fachada));
						}
					});
				}
			});
		} 
		itensEncontrados.add(panel);
	}
	
	private void eventoDeMouseOver(final Label label) {
		label.addMouseOverHandler(new MouseOverHandler() {	
			@Override
			public void onMouseOver(MouseOverEvent event) {
				label.setStyleName("underlined");
			}
		});
		label.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				label.setStyleName("gwt-Label");								
			}
		});
	}
	
	private void iniciaLabels() {
		Label lblOrdenao = new Label("Ordena\u00E7\u00E3o: ");
		absolutePanelCaption.add(lblOrdenao, 390, 12);
		Label lblCritrio = new Label("Crit\u00E9rio: ");
		absolutePanelCaption.add(lblCritrio, 414, 54);
		Label lblPesquisarPor = new Label("Pesquisar por: ");
		absolutePanelCaption.add(lblPesquisarPor, 10, 53);
		Label lblPalavraChave = new Label("Palavra chave: ");
		absolutePanelCaption.add(lblPalavraChave, 10, 11);
		Label lblItensEncontrados = new Label("Itens encontrados:");
		lblItensEncontrados.setStyleName("caption-panel");
		absolutePanelCaption.add(lblItensEncontrados, 10, 103);
	}
}